package test.n00400;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Problem00460 {
    public static void main(String[] args) {
        println(new int[]{1, 2, 3});
    }

    public static void println(int[] nums) {
        Solution ss = new Solution();
        System.out.println(Arrays.asList(nums) + "," + ss.permute(nums));
    }

    public static class Solution {

        /**
         * 给定一个不含重复数字的数组 nums ，返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
         * <p>
         * 解题思路:
         * 回溯
         *
         * @param nums
         * @return
         */
        public List<List<Integer>> permute(int[] nums) {
            List<List<Integer>> result = new ArrayList<>();
            final List<Integer> output = Arrays.stream(nums).boxed().collect(Collectors.toList());
            permute(nums.length, 0, output, result);
            return result;
        }

        public void permute(int n, int first, List<Integer> output, List<List<Integer>> result) {
            if (n == first) {
                result.add(new ArrayList<>(output));
                return;
            }

            for (int i = first; i < n; i++) {
                Collections.swap(output, first, i);
                permute(n, first + 1, output, result);
                Collections.swap(output, first, i);
            }
        }
    }
}
